/*
 * Copyright (C) 2012 Matt Broadstone
 * Contact: http://code.google.com/p/qconnman/
 *
 * This file is part of the QConnman Library.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

#include <QRegExpValidator>
#include <QDesktopServices>
#include <QMessageBox>
#include <QInputDialog>

#include "manager.h"
#include "agentadaptor.h"
#include "agent.h"

#define 	ASCII_MIN   0
#define 	ASCII_MAX   255

Agent::Agent(const QDBusObjectPath &path, Manager *parent)
    : QObject(parent),
      m_manager(parent),
      m_adaptor(0),
      m_path(path)
{
    m_adaptor = new AgentAdaptor(this);
}

Agent::Agent(const QString &path, Manager *parent)
    : QObject(parent),
      m_manager(parent),
      m_adaptor(0),
      m_path(QDBusObjectPath(path))
{
    m_adaptor = new AgentAdaptor(this);
    setupValidators();
}

Agent::~Agent()
{
}

QDBusObjectPath Agent::path() const
{
    return m_path;
}

Manager *Agent::manager() const
{
    return m_manager;
}

void Agent::setupValidators()
{
    // WEP validator
/*
    QString hexRegExp = "([0-9]|[a-f]|[A-F]){10}|([0-9]|[a-f]|[A-F]){26}"; // 10 or 26 Hex digits
    QString asciiRegExp = "[-";
    for (char ch = ASCII_MIN; ch <= ASCII_MAX; ++ch) {
        if (ch != '-') {
            if (ch == '\\' || ch == ']') {
                asciiRegExp.append('\\');
            }

            asciiRegExp.append('\\');
        }
    }
    asciiRegExp.append("]");
    asciiRegExp = asciiRegExp + "{5}|" + asciiRegExp + "{13}";  // 5 or 13 ASCII characters
    QRegExp wepRegExp(QString("^(%1|%2)$").arg(hexRegExp).arg(asciiRegExp));
    m_wepValidator = new QRegExpValidator(wepRegExp, this);
*/
}



void Agent::Cancel()
{
    qDebug() << Q_FUNC_INFO << "called";
}

void Agent::Release()
{
    qDebug() << Q_FUNC_INFO << "called";
}

void Agent::ReportError(const QDBusObjectPath &service, const QString &error)
{
    qDebug() << Q_FUNC_INFO << "for service: " << service.path();
    QMessageBox::critical(0, tr("Error"), error);
}

void Agent::RequestBrowser(const QDBusObjectPath &service, const QString &url)
{
    qDebug() << Q_FUNC_INFO << endl
             << "\tpath: " << service.path() << endl
             << "\t url: " << url;

    if (QDesktopServices::openUrl(QUrl::fromUserInput(url))) {
        qDebug() << "\tsuccess";
    } else {
        qDebug() << "\tfailed";
    }
}

QVariantMap Agent::RequestInput(const QDBusObjectPath &servicePath, const QVariantMap &fields)
{
//    qDebug() << Q_FUNC_INFO << "for service: " << servicePath.path();
    if (!m_manager->hasService(servicePath)) {
        qDebug() << "\tinvalid service";
        return QVariantMap();
    }

    QVariantMap result;
//    Service *service = m_manager->service(servicePath);
    if (fields.contains(QLatin1String("Passphrase"))) {
        /*
        if (service->security().contains("wep")) {
            dialog->setInputMask
        }
        */

        QString passphrase = QInputDialog::getText(0, "enter passphrase", "enter passphrase");
        result.insert("Passphrase", passphrase);
    }

//    foreach (QString key, fields.keys()) {
//        QDBusArgument arg = fields.value(key).value<QDBusArgument>();
//        qDebug() << "\t" << key << " = " << arg.asVariant();
//    }

    return result;
}
